#include <tsin/library/BulletCircleDirector.hpp>

#include <tsin/runtime/BulletRuntime.hpp>
#include <tsin/util/Logger.hpp>

#include <cstring>
#define _USE_MATH_DEFINES
#include <cmath>

#include <tsin/util/MemCheck.hpp>
using namespace tsin;

DynamicObject *BulletCircleDirector::copy()
{
        return DynamicObject::copy(new BulletCircleDirector);
}

void BulletCircleDirector::init(int n, bool isClockWise, float start)
{
        this->interval = 2 * M_PI / n;
        this->start = start;
        this->isClockwise = isClockwise;
}

void BulletCircleDirector::init(float start)
{
        this->interval = 0.0f;
        this->start = start;
}

void BulletCircleDirector::reinit()
{
        int   arg1i = arguments[0]->getInt();
        float arg1f = arguments[0]->getFloatInRad();
        bool  arg2  = arguments[1]->getBool();
        float arg3  = arguments[2]->getFloatInRad();

        switch(nrArguments) {
        case 1:
                if (strcmp(desp, "N") == 0)
                        init(arg1f);
                break;
        case 2:
                if (strcmp(desp, "NB") == 0)
                        init(arg1i, arg2);
                break;
        case 3:
                if (strcmp(desp, "NBN") == 0)
                        init(arg1i, arg2, arg3);
                break;
        }
}

void BulletCircleDirector::reset()
{
        count = 0;
}

void BulletCircleDirector::direct(BulletRuntime *bullet, void *)
{
        if (interval != 0.0f)
                bullet->direction = start + (isClockwise ? interval : - interval) * count;
        else
                bullet->direction = bullet->angle + start;
        count ++;
}

DynamicObject *bindCircleDirector()
{
        return new BulletCircleDirector;
}
